#include "timer.h"
#include <pc.h>
#include <stdio.h>


extern int sec_tick;
extern int time_tick;


/** Program timer (0,1,2) with mode
 */
void timer_init(int timer, int mode)
{
	//outportb(timer,mode);
	int ntimer =timer;
	outportb(TIMER_CTRL,(ntimer <<6)|mode);
}


/** Load timer (0,1,2) with value
 */
void timer_load(int timer, int value)
{
	int ntimer;
	
	switch(timer){
	
	case 0:ntimer =TIMER_0;break;
	case 1:return;
	case 2:ntimer = TIMER_2;break;
	default: return;
	}
	
	
	//Coloca primeiro os bits menos significativos de VALUE no timer
	outportb(ntimer, LSB(value));
	//Coloca finalmente os bits mais significativos de Value no timer
	outportb(ntimer, MSB(value));
}

/** Wait mili miliseconds, indirectly counting T0 interrupts
*/
void mili_sleep(int mili)
{
	 int time = time_tick +mili;
	 
	 while(time_tick < time)
	 {
		/*printf("time_tick %d \n", time_tick);*/
	 }
}

/** wait secs seconds, indirectly counting T0 interrupts
 */
void sec_sleep(int secs)
{
	mili_sleep( secs*1000 );
}

/** @} end of TimerCounter */
